package com.yng.simbot01.service;

import com.yng.simbot01.AiChat.AiChatInter;
import com.yng.simbot01.ws.MessageListener;
import com.yng.simbot01.ws.WebSocket;
import love.forte.simbot.event.MessageEvent;
import love.forte.simbot.message.MessageContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.concurrent.CompletableFuture;

@Component
public class ChatEventService implements MessageListener {
    @Autowired
    private AiChatInter aiChatInter;
    @Autowired
    private WebSocket webSocket;

    private static String re = null;

    //消息处理 走AI
    public CompletableFuture<?> chatWithKeyWordAndAI(MessageEvent event) {
        re = null;
        MessageContent messageContent = event.getMessageContent();
        String plainText = messageContent.getPlainText();
        //String result = aiChatInter.chat(plainText);
        webSocket.sendMessage(plainText);
        while (re == null) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return event.replyAsync(re);
    }


    @Override
    public void onMessageReceived(String data) {
        System.out.println(data);
        re = data;
    }
}
